package com.atguigu.config.websocket1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 开启WebSocket支持
 * @author zhengkai
 */
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer{
//	@Bean
//    public ServerEndpointExporter serverEndpointExporter() {  
//        return new ServerEndpointExporter();  
//    }  
	@Autowired
	MyWebSocketHandler myHandler;
	
	@Autowired
	MyHandshakeInterceptor myInterceptor;
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
		//这里的url要与页面的url一致
		webSocketHandlerRegistry.addHandler(myHandler,"/onlineUser").addInterceptors(myInterceptor);
		//至于这里为什么要加info，我遇见的情况是，当我使用sockjs来代替websocket时，连接的后面会自动加上info
		webSocketHandlerRegistry.addHandler(myHandler,"/sockjs/onlineUser/info").addInterceptors(myInterceptor).withSockJS();
	}
//	@Bean
//	public WebSocketHandler myHandler(){
//		return new MyWebSocketHandler();
//	}
}
